package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gcxy.entity.Role;
import com.gcxy.entity.UserRole;
import com.gcxy.mapper.UserRoleMapper;
import com.gcxy.service.RoleService;
import com.gcxy.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.utils.BeanCopyUtil;
import com.gcxy.vo.role.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author Huang先森
 * @since 2024-07-22
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据用户id删除用户角色关联关系
     *
     * @param userId
     */
    @Override
    public void removeByUserId(Integer userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId);
        boolean remove = remove(queryWrapper);

    }

    /**
     * 根据角色姓名获取用户信息
     *
     * @param roleName
     * @return
     */
    @Override
    public List<Integer> getUserByRoleName(String roleName) {
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>().like(Role::getRoleName, roleName));
        if (CollectionUtils.isNotEmpty(roles)) {
            List<Integer> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
            List<UserRole> userRoles = getByRoleIds(roleIds);
            if (CollectionUtils.isNotEmpty(userRoles)) {
                return userRoles.stream().map(UserRole::getUserId).collect(Collectors.toList());
            }
            return new ArrayList<>();
        }
        return new ArrayList<>();
    }

    /**
     * 根据角色id获取用户角色关联关系
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<UserRole> getByRoleIds(List<Integer> roleIds) {
        return list(new LambdaQueryWrapper<UserRole>().in(UserRole::getRoleId, roleIds));
    }

    /**
     * 根据用户id获取角色信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<RoleVO> getRoleByUserId(Integer userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId);
        List<UserRole> userRoleList = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(userRoleList)) {
            List<Role> roleList = roleService.list(new LambdaQueryWrapper<Role>().in(Role::getId, userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList())));
            List<RoleVO> roleVOList = BeanCopyUtil.copyList(roleList, RoleVO.class);
            return roleVOList;
        }
        return new ArrayList<>();
    }

    /**
     * 根据角色id获取所有关联用户信息
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getAllUserByRoleId(Integer roleId) {
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, roleId);
        List<UserRole> userRolesList = list(userRoleLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(userRolesList)) {
            return new ArrayList<>();
        }

        return userRolesList.stream().map(UserRole::getUserId).collect(Collectors.toList());
    }

    /**
     * 插入用户角色关联关系
     *
     * @param userId
     * @param roleId
     * @return
     */
    public boolean insertUserRole(Integer userId, Integer roleId){
        LambdaQueryWrapper<UserRole> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(UserRole::getUserId,userId).eq(UserRole::getRoleId,roleId);
        if (count(lambdaQuery) == 0){
            List<UserRole> userRoles = userRoleMapper.selectList(lambdaQuery);
            if (CollectionUtils.isEmpty(userRoles)){
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(userId);
                return save(userRole);
            }
        }
        return false;
    }
}
